package cn.xudy.top.controller;

import cn.xudy.top.model.RedisModel;
import cn.xudy.top.service.Impl.RedisServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ulegal on 2017/11/7.
 */
@RestController
public class RedisController {

    @Autowired
    private RedisServiceImpl service;

    @GetMapping(value = "/session")
    public String sessionAction(HttpSession session){

        //使用request对象的getSession()获取session，如果session不存在则创建一个
//        HttpSession session = request.getSession();
        //将数据存储到session中
        session.setAttribute("data", "Session 共享 9966");
       String name = "端口号：9966========="+ session.getAttribute("data");
        System.out.println("端口号：9966========="+name);
        return name;
    }

    @GetMapping(value = "/list")
    public String ListPoo(){

        List<RedisModel> list = new ArrayList<>();
        System.out.println("start.....");
        RedisModel m = new RedisModel();
        m.setName("张三1");
        m.setTel("1111");
        m.setAddress("深圳1");
        m.setRedisKey("zhangsanKey01");
        list.add(m);

        RedisModel m2 = new RedisModel();
        m2.setName("张三2");
        m2.setTel("2222");
        m2.setAddress("深圳2");
        m2.setRedisKey("zhangsanKey02");
        list.add(m2);

        service.putList("zhangsanKey01", list, -1);

        return "你好";

    }

    @GetMapping(value = "/getList")
    public List<RedisModel> getpopList(){

        List<RedisModel> list = (List<RedisModel>) service.getputPop("zhangsanKey01");

        return list;
    }

    //添加
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public void test() {

        System.out.println("start.....");
        RedisModel m = new RedisModel();
        m.setName("张三");
        m.setTel("1111");
        m.setAddress("深圳1");
        m.setRedisKey("zhangsanKey01");
        service.put(m.getRedisKey(), m, -1);

        RedisModel m2 = new RedisModel();
        m2.setName("张三2");
        m2.setTel("2222");
        m2.setAddress("深圳2");
        m2.setRedisKey("zhangsanKey02");
        service.put(m2.getRedisKey(), m2, -1);

        RedisModel m3 = new RedisModel();
        m3.setName("张三3");
        m3.setTel("2222");
        m3.setAddress("深圳2");
        m3.setRedisKey("zhangsanKey03");
        service.put(m3.getRedisKey(), m3, -1);

        System.out.println("add success end...");
    }

    //查询所有对象
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public Object getAll() {
        return service.getAll();
    }

    //查询所有key
    @RequestMapping(value = "/getKeys", method = RequestMethod.GET)
    @ResponseBody
    public Object getKeys() {
        return service.getKeys();
    }

    //根据key查询
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Object get(String key) {
        RedisModel m = new RedisModel();
        m.setRedisKey(key);
        return service.get(m.getRedisKey());
    }

    //删除
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ResponseBody
    public void remove() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        service.remove(m.getRedisKey());
    }

    //判断key是否存在
    @RequestMapping(value = "/isKeyExists", method = RequestMethod.GET)
    @ResponseBody
    public void isKeyExists() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        boolean flag = service.isKeyExists(m.getRedisKey());
        System.out.println("zhangsanKey01 是否存在: "+flag);
    }

    //查询当前缓存的数量
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ResponseBody
    public Object count() {
        return service.count();
    }

    //清空所有key
    @RequestMapping(value = "/empty", method = RequestMethod.GET)
    @ResponseBody
    public void empty() {
        service.empty();
    }

}
